<?php 
namespace App\Models\Authority;

use App\Models\Base\AuthorityBase;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Auth\Authorizable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Users extends AuthorityBase implements AuthenticatableContract, AuthorizableContract, JWTSubject
{
    use Authenticatable, Authorizable;

    protected $table = 'uc_users';
    protected $hidden = ['password', 'user_code', 'status', 'cardID'];
    protected $fillable = [];
    protected $dates = [];
    protected $primaryKey = 'usersid';
    protected $keyType = 'string';
    public $incrementing = false;
    public static $loginRules = [
        // Validation rules
        'password' => 'required|min:6|max:20',
    ];
    public static $regRules = [
        'password' => 'required|min:6|max:20',
    ];
    public static $message = [];
    public static $custom = [];
    // Relationships

    /**
     * JWT
     *
     * @author AdamTyn
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * JWT
     *
     * @author AdamTyn
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 获取用户可访问接口权限
     *
     * @param [type] $usersid 用户uuid
     * @return void
     */
    public function PowerUrl($usersid)
    {
        $url = DB::table('uc_interface')
            ->join('uc_users_interface', 'uc_interface.interfaceid', '=', 'uc_users_interface.interface_id')
            ->join('uc_users', 'uc_users.usersid', '=', 'uc_users_interface.users_id')
            ->where('uc_users.usersid', '=', $usersid)
            ->pluck('url');
        $url = $url->merge(DB::table('uc_interface')
                ->join('uc_roles_interface', 'uc_roles_interface.interface_id', '=', 'uc_interface.interfaceid')
                ->join('uc_roles', 'uc_roles.rolesid', '=', 'uc_roles_interface.roles_id')
                ->join('uc_roles_users', 'uc_roles_users.roles_id', '=', 'uc_roles.rolesid')
                ->join('uc_users', 'uc_users.usersid', '=', 'uc_roles_users.users_id')
                ->where('uc_users.usersid', '=', $usersid)
                ->pluck('url'))->toArray();
        return implode(',', array_unique($url));
    }

    /**
     * 获取用户的角色和回调函数
     *
     * @param [type] $userid 用户uuid
     * @param [type] $key 传入系统公钥
     * @return void
     */
    public function PowerRole($userid, $key)
    {
        $rt = DB::table('uc_users')
            ->join('uc_roles_users', 'uc_users.usersid', '=', 'uc_roles_users.users_id')
            ->join('uc_keys_roles', 'uc_roles_users.roles_id', '=', 'uc_keys_roles.roles_id')
            ->join('uc_sys_keys', 'uc_keys_roles.keys_id', '=', 'uc_sys_keys.sysid')
            ->join('uc_roles', 'uc_roles.rolesid', '=', 'uc_keys_roles.roles_id')
            ->where('uc_users.usersid', '=', $userid)
            ->where('uc_sys_keys.sys_key', '=', $key)
            ->pluck('rolesid')->toArray();
        return $rt;
    }
}
